//
//  HWLiBaoCenterViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/1.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWLiBaoCenterViewController.h"
#import "HWLiBaoCenterCell.h"
#import "CustomHeadFooterView.h"
#import "HWGroupModel.h"
#import "HWLiBaoModel.h"
#import "HWsousuLibaoViewController.h"

@interface HWLiBaoCenterViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,CustomHeadFooterViewDelegate,HWLiBaoCenterCellDelegate>
@property(strong,nonatomic)UITableView *mainaTableVie;
@property(strong,nonatomic) UIView *seacherView; // 搜索Uiview
@property(strong,nonatomic) UITextField *searchTextField;
@property(strong,nonatomic) NSMutableArray *groups;



@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;

@end

@implementation HWLiBaoCenterViewController

-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {

         _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, self.seacherView.bottom, kScreenW,kScreenH-self.seacherView.bottom) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
         _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.sectionHeaderHeight = 71;
        

    }
    return   _mainaTableVie ;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
}
// 礼包中心
- (void)setupUI  {
    
    self.navigationItem.title = @"礼包中心";
    self.navigationItem.title = [HWjianzhiduiObject keylocalization:@"礼包中心"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
   // [self addDatashuju];
    
    [self addseacherView];
    [self.view addSubview:self.mainaTableVie];

    [self addrefreshControl];
}



#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {
    
   
    
    self.p = @"1";
     
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];

    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"lb",
                                      
                                       };
       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
              self.groups = [HWGroupModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               
               
               
               [self.mainaTableVie reloadData];
               
    
           }
        
           
           
       } failure:^(NSString *error) {
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 上拉加载

- (void)onRefresh {
    
    
   
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
         [self.mainaTableVie.mj_header endRefreshing];
         [self.mainaTableVie.mj_footer endRefreshing];
                    
                       
        return ;
                       
      }
                   
    
    
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
     NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     
     NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
     
     NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
     
     NSString *singMD5 = [WMUtil md5HexDigest:singStr];

  
     NSDictionary *parametes = @{
                                         @"p" :self.p,
                                         @"ios" : @"1",
                                         @"ct" :  @"app",
                                         @"ac" : @"lb",
                                       
                                         };
         
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
             
               
                
                 NSMutableArray *mutableArr = [HWGroupModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];


                 for (int i = 0; i < mutableArr.count; i++) {
                     [  self.groups addObject:mutableArr[i]];

                 }
                 
                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
              
                 
      
             }
          
                [self.mainaTableVie reloadData];
             
         } failure:^(NSString *error) {
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
         }];
    
    
    
    
}














// 添加数据
- (void)addDatashuju {
    
    NSDictionary *dic1 = @{@"name":@"幻想骑士团",@"online":@"该游戏礼包有三个",@"visible":@"NO",@"friends":@[@{@"libaoStr":@"iioijo2"},@{@"libaoStr":@"加速卡*2"},@{@"libaoStr":@"礼包简介2：经验加速卡*2"}]};
       NSDictionary *dic2 = @{@"name":@"幻想骑士团",@"online":@"该游戏礼包有三个",@"visible":@"NO",@"friends":@[@{@"libaoStr":@"礼包简介：经验加速卡*2"},@{@"libaoStr":@"礼包简介2：经验加速卡*2"},@{@"libaoStr":@"礼包简介2：经验加速卡*2"}]};
       NSDictionary *dic3 = @{@"name":@"幻想骑士团",@"online":@"该游戏礼包有三个",@"visible":@"NO",@"friends":@[@{@"libaoStr":@"礼包简介：经验加速卡*2"},@{@"libaoStr":@"礼包简介2：经验加速卡*2"},@{@"libaoStr":@"礼包简介2：经验加速卡*2"}]};
    
    
     HWGroupModel *group1 = [HWGroupModel mj_objectWithKeyValues:dic1];
     HWGroupModel *group2 = [HWGroupModel mj_objectWithKeyValues:dic2];
     HWGroupModel *group3 = [HWGroupModel mj_objectWithKeyValues:dic3];
    
    
    self.groups = [[NSMutableArray alloc]init];
    [self.groups addObject:group1];
     [self.groups addObject:group2];
    [self.groups addObject:group3];
    
}




- (void)addseacherView {
    
    
    self.seacherView = [[UIView alloc]initWithFrame:CGRectMake(0, NavBarHeight, kScreenW, 44+6)];
    self.seacherView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.seacherView];
    
    
       UIView *sousuoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 32)];
       UITextField *texfeild = [[UITextField alloc]initWithFrame:CGRectMake(15, 6 , kScreenW-15*2, 32)];
       texfeild.returnKeyType =UIReturnKeySearch;
       texfeild.delegate = self;
       texfeild.placeholder = @"输入你要搜索的游戏";
    texfeild.placeholder  =  [HWjianzhiduiObject keylocalization:@"输入你要搜索的游戏"];
       //    texfeild.layer.borderColor  = RGB(208, 208, 208).CGColor;
       //    texfeild.layer.borderWidth = 0.5;
       texfeild.font = [UIFont systemFontOfSize:12];
      
       
//        UIView *sousuoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 32)];
       UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
       button.frame = CGRectMake(5, 0, 30, 30);
       [button setImage:[UIImage imageNamed:@"sousukuananniu_ico"] forState:UIControlStateNormal];
       [button setImage:[UIImage imageNamed:@"sousukuananniu_ico"] forState:UIControlStateHighlighted];
      [sousuoView addSubview:button];
    
    
       texfeild.leftView = sousuoView;
       texfeild.leftViewMode = UITextFieldViewModeAlways;
       texfeild.layer.cornerRadius = 16;
       texfeild.layer.masksToBounds = YES;
       texfeild.backgroundColor = RGB(244, 242, 245);
      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(popUpSearchView)];
       [texfeild addGestureRecognizer:tap];
       //   [texfeild becomeFirstResponder];
       [self.seacherView addSubview:texfeild];
       
    
       self.searchTextField = texfeild;
    
     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 44, kScreenW, 6)];
     view.backgroundColor = RGB(244, 242, 245);
     [self.seacherView addSubview:view];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.groups.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
     HWGroupModel *group = self.groups[section];
    
    if (group.visible) {
        return group.list.count;
        
        
    }else{
        return 0;
    }
    

    
}

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    
    
    HWLiBaoCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
        cell = [[HWLiBaoCenterCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
     cell.backgroundColor = RGB(244, 242, 245);
    cell.delegate = self;
    HWGroupModel *group = self.groups[indexPath.section];
    HWLiBaoModel *friend = group.list[indexPath.row];
    
    
    cell.friendModel = friend;
   // cell.backgroundColor =  [UIColor redColor]; 
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    
    return  cell;
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return  36;
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   HWGroupModel  *group = self.groups[section];
   CustomHeadFooterView *view = [CustomHeadFooterView headFooterViewWithTableview:tableView];
    view.delegate = self;
    view.group = group;
    view.tag = section;
   // view.backgroundColor = [UIColor redColor];
    //return 返回之前headerFooterView的frame是0,所以需要在某个地方设置headerFooterView的frame
    return view;
    //return 返回之后，uitableview在用headerFooterView的时候就会设置headerFooterView的frame
}


- (void)groupHeaderViewDidClickTitleButton:(CustomHeadFooterView *)headerview{
    //全局刷新
//    [self.tv reloadData];
    //局部刷新
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:headerview.tag];
    [self.mainaTableVie reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];

}


#pragma mark --------  点击领取的代理
- (void)HWLiBaoCenterxuanzhongCell:(HWLiBaoCenterCell *)lingqude {
    
    NSIndexPath   *indexPath = [self.mainaTableVie indexPathForCell:lingqude];
    
    
    HWGroupModel *group = self.groups[indexPath.section];
     HWLiBaoModel *friend = group.list[indexPath.row];
    
    
      NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    
      int r =  [WMUtil getRandomNumber:101 to:998];
      NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],r];
      
    
      NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
      
      NSString *singMD5 = [WMUtil md5HexDigest:singStr];

      self.dataMuArr = [[NSMutableArray alloc]init];
      NSDictionary *parametes = @{
                                      
                                         @"ios" : @"1",
                                         @"ct" :  @"app",
                                         @"ac" : @"get_lb",
                                         @"uid" :uidStr,
                                         @"t" : tStr,
                                         @"sign" : singMD5,
                                         @"id"  :friend.id
                                         };
         
         [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
     
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
         
                     [MBProgressHUD showError:response[@"message"]];
                 
                 
              
                 
      
             } else {
                 
                 [MBProgressHUD showError:response[@"message"]];
                                 
                 
             }
          
             
             
         } failure:^(NSString *error) {
             
                 [MBProgressHUD showError:@"领取失败"];
             
         }];
    
    
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    return  NO;
}

- (void)popUpSearchView {
    
    
    HWsousuLibaoViewController *sou = [[HWsousuLibaoViewController alloc]init];
    [self.navigationController pushViewController:sou animated:YES];
    
    
    
}
 


@end
